package com.bit.talkoftoday;

import java.io.File;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.RemoteViews;

public class ImgWidget extends AppWidgetProvider{
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		if(intent.getAction().equals("android.appwidget.action.APPWIDGET_CHANGE")){
			ComponentName componentname = new ComponentName(context, ImgWidget.class);
	        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
	        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentname);
			change(context, appWidgetManager, appWidgetIds);
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		change(context, appWidgetManager, appWidgetIds);
	}
	
	public void change(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds){
		String imgPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/rhemacard.jpg";
		RemoteViews remote = new RemoteViews(context.getPackageName(),R.layout.nalyoil);
//		File file = new File(imgPath);
//		Uri imguUri = Uri.fromFile(file);
//		remote.setImageViewUri(R.id.img_widget, imguUri);
		File file = new File(imgPath);
		if(file.exists() == false){
			return;
		}
		BitmapFactory.Options option = new BitmapFactory.Options();
		option.inSampleSize = 1;
		option.inPurgeable = true;
		option.inDither = true;
		
		Bitmap bitmap = BitmapFactory.decodeFile(imgPath,option);
		remote.setImageViewBitmap(R.id.img_widget, bitmap);
		if(appWidgetIds != null && appWidgetIds.length != 0){
			for (int appId : appWidgetIds) {
				appWidgetManager.updateAppWidget(appId, remote);
			}
		}
		bitmap.recycle();
	}
}
